<?php

/**
 * 错误接管类
 */

namespace variant\core;

class Exceptions {

	/**
	 * 错误提示列表
	 * @var array
	 */
	public $levels = array(
		E_ERROR             => 'Error',
        E_WARNING           => 'Warning',
        E_PARSE             => 'Parse error',
        E_NOTICE            => 'Notice',
        E_CORE_ERROR        => 'Core error',
        E_CORE_WARNING      => 'Core warning',
        E_COMPILE_ERROR     => 'Compile error',
        E_COMPILE_WARNING   => 'Compile warning',
        E_USER_ERROR        => 'User error',
        E_USER_WARNING      => 'User warning',
        E_USER_NOTICE       => 'User notice',
        E_STRICT            => 'Strict warning',
        E_RECOVERABLE_ERROR => 'Recoverable fatal error',
        E_DEPRECATED        => 'Deprecated function',
        E_USER_DEPRECATED   => 'User deprecated function',
	);


	/**
	 * 实例化类
	 */
	public function __construct() {
		set_error_handler(array($this, 'error_handler'));
    	set_exception_handler(array($this, 'exception_handler'));

	}

	/**
	 * 错误接管
	 */
	public function error_handler($error_level, $message, $filename, $line) {
		if(!Config::get('EXCEPTIONS.DEBUG_STRICT')){
			if( $error_level == E_NOTICE || $error_level == E_STRICT || $error_level == E_WARNING) return true;
		}
	    $message = $this->levels[$error_level] . ' : ' . $message . ' <code> line '.$line.' in file '.$filename.'</code>';
	    if (!headers_sent()) {
			header('HTTP/1.1 500 Internal Server Error');  
		}
		$this->_handler($message, debug_backtrace());
	}

	/**
	 * 异常接管
	 */
	public function exception_handler($exception) {
		$code = $exception->getCode();
		if($code == '404'){
			$this->error_404('You visit the page does not exist!');
		}
		$message = $exception->getMessage();
		if (empty($message)) {
			$message = '(null)';
		}else{
			$message = $message . ' <code> line '.$exception->getLine().' in file '.$exception->getFile().'</code>';
		}
		$this->_handler($message, $exception->getTrace());
		
	}

	/**
	 * 异常处理
	 */
	protected function _handler($message, $trace) {

		if (!headers_sent()) {
			header('HTTP/1.1 500 Internal Server Error');  
		}
		if(config('EXCEPTIONS.DEBUG')){
			$message .= '<strong>Trace</strong> <code>';
			foreach ($trace as $value) {
				$message  .= $value['file'] . ' line ' . $value['line'] . '<br>';
			}
			$message .= '</code>';
			//自定义记录
			$log = debug_log();
			if(!empty($log)){
				foreach ((array)$log as $name => $list) {
					if(!empty($list)){
						$list = array_reverse($list);
						$message .= '<strong>'.$name.'</strong> <code>';
							foreach ((array)$list as $value) {
							$message  .= $value['log'] . ' time ' . $value['time'] . ' ms<br>';
							}
						$message .= '</code>';
					}
				}
			}
		}else{
			$message = 'Sorry, the page you visit has a problem, you can take the information back to the administrator, so that we can quickly resolve the problem!';
		}
		$this->error_show($message);
	}

	/**
	 * 页面不存在
	 */
	public function error_404($message){
		header('HTTP/1.1 404 Not Found');
		header("status: 404 Not Found");
		$this->error_show($message);
	}

	/**
	 * 错误输出
	 */
	public function error_show($message, $heading = 'Internal Server Error') {
		$enging = Config::get('EXCEPTIONS.ENGINE');
		if(!empty($enging)){
			new $enging->errorMsg($message);
		}else{
			ob_start();
			include(VAT_PATH . 'tpl' . DS . 'error_exception.php');
			$buffer = ob_get_contents();
			ob_end_clean();
			echo $buffer;
		}
		exit;
	}
}